package com.easpeed.comon.utils

import com.blankj.utilcode.util.SPUtils

/**
 * Created by bigyu on 2025-04-07 13:22
 * com.easpeed.comon.utils
 */
class CacheHelper {
    companion object {
        private var instance: CacheHelper? = null
        private var spUtils: SPUtils? = null

        @JvmStatic
        fun instance(): CacheHelper {
            if (instance == null) {
                spUtils = SPUtils("cache")
                instance = CacheHelper()
            }
            return CacheHelper()
        }
    }

    fun save(key: String, value: Any?) {
        if (value is String?) {
            spUtils?.put(key, value)
        } else if (value is Int) {
            spUtils?.put(key, value)
        } else if (value is Boolean) {
            spUtils?.put(key, value)
        } else if (value is Float) {
            spUtils?.put(key, value)
        } else if (value is Long) {
            spUtils?.put(key, value)
        }
    }

    fun getStringValue(key: String, defaultValue: String? = ""): String? {
        return spUtils?.getString(key, defaultValue)
    }

    fun getIntValue(key: String, defaultValue: Int = 0): Int {
        return spUtils?.getInt(key, defaultValue) ?: defaultValue
    }



    fun remove(key: String) {
        spUtils?.remove(key)
    }

    @JvmOverloads
    fun getBooleanValue(key: String, defaultValue: Boolean = false): Boolean {
        return spUtils?.getBoolean(key, defaultValue) ?: defaultValue
    }
}

